package com.mo.axion.service.api.impl.action.send;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.mo.axion.common.enums.RespStatusEnum;
import com.mo.axion.common.pipeline.BusinessProcess;
import com.mo.axion.common.pipeline.ProcessContext;
import com.mo.axion.common.vo.BasicResultVO;
import com.mo.axion.domain.MessageParam;
import com.mo.axion.service.api.impl.domain.SendTaskModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Mo
 * @createTime 2024/2/1 17:55
 * @description 1.发送消息入参校验
 */
@Slf4j
@Service
public class SendPreCheckAction implements BusinessProcess<SendTaskModel> {
    /**
     * 真正处理逻辑
     *
     * @param context 处理上下文
     */
    @Override
    public void process(ProcessContext<SendTaskModel> context) {
        SendTaskModel sendTaskModel = context.getProcessModel();
        Long messageTemplateId = sendTaskModel.getMessageTemplateId();
        List<MessageParam> messageParamList = sendTaskModel.getMessageParamList();
        //  1. 检查是否传入 消息模板Id 或 messageParam
        if (Objects.isNull(messageTemplateId) || CollUtil.isEmpty(messageParamList)) {
            context.setIsNeedBreak(true)
                    .setResponse(
                            BasicResultVO.fail(RespStatusEnum.CLIENT_BAD_PARAMETERS, "模板Id或参数列表为空")
                    );
            return;
        }
        // 2. 过滤 receiver 为空的messageParam
        List<MessageParam> resultMessageParamList = messageParamList.stream()
                .filter(messageParam -> !StrUtil.isBlank(messageParam.getReceiver()))
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(resultMessageParamList)) {
            context.setIsNeedBreak(true)
                    .setResponse(
                            BasicResultVO.fail(RespStatusEnum.CLIENT_BAD_PARAMETERS, "含接收者的参数列表为空")
                    );
            return;
        }
        sendTaskModel.setMessageParamList(resultMessageParamList);
    }
}
